﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using com.lover.astd.common.config;
using com.lover.astd.common.manager;
using com.lover.astd.common.model;
using com.lover.astd.common.model.enumer;
using com.lover.common;

namespace com.lover.astd.selector
{
    public partial class Selector : Form
    {
        private bool _intentExit = false;
        private class SMeta
        {
            public SMeta(ServerType tp, string desc) { _stype = tp; _desc = desc; }
            private ServerType _stype;
            public ServerType Stype { get { return _stype; } set { _stype = value; } }
            private string _desc;
            public string Desc { get { return _desc; } set { _desc = value; } }
        }
        private List<SMeta> _serverTypes;
        private List<AccountData> _users;
        public Selector()
        {
            InitializeComponent();
            GameMgr.setSelector(this);
        }

        private void Selector_Load(object sender, EventArgs e)
        {
            // load all accounts
            _users = new List<AccountData>();
            _serverTypes = new List<SMeta>();
            foreach (ServerType tp in Enum.GetValues(typeof(ServerType)))
            {
                _serverTypes.Add(new SMeta(tp, EnumString.getString(tp)));
            }
            combo_servertype.DataSource = _serverTypes;
            combo_servertype.DisplayMember = "Desc";
            loadAccounts();
            lst_accounts.DataSource = _users;
        }
        private void loadAccounts()
        {
            GlobalConfig.getInstance().loadSettings();
            _users = GlobalConfig.getInstance().getAllAccounts();
            // init all account games
            foreach (AccountData dt in _users)
            {
                GameMgr.addGame(dt);
            }
        }
        public void refreshStatus()
        {
            lst_accounts.DataSource = null;
            lst_accounts.DataSource = _users;
        }
        private void btn_add_Click(object sender, EventArgs e)
        {
            if (combo_servertype.SelectedIndex < 0)
            {
                UiUtils.getInstance().info("大虾，表糊弄我了，表还没填完呢，想进去免谈！！");
                return;
            }
            SMeta stype_meta = combo_servertype.SelectedItem as SMeta;
            if (stype_meta.Stype == ServerType.Custom)
            {
                UiUtils.getInstance().info("大虾，表糊弄我了，表还没填完呢！！");
                return;
            }
            if (num_serverid.Value <= 0 || txt_username.Text == "" || txt_password.Text == "")
            {
                UiUtils.getInstance().info("大虾，表糊弄我了，表还没填完呢！！");
                return;
            }

            int serverId = (int)(num_serverid.Value);
            string roleName = txt_rolename.Text;
            string username = txt_username.Text;
            string password = txt_password.Text;
            AccountData acc = new AccountData();
            acc.Server_type = stype_meta.Stype;
            acc.ServerId = serverId;
            acc.UserName = username;
            acc.Password = password;
            acc.RoleName = roleName;
            bool found = false;
            foreach (AccountData dt in _users)
            {
                if (dt.Equals(acc))
                {
                    found = true;
                    break;
                }
            }
            if (found)
            {
                UiUtils.getInstance().info("已经有这个账号了, 别再加了!");
                return;
            }
            GameMgr.addGame(acc);
            GlobalConfig.getInstance().addAccount(acc);
        }

        private void btn_change_Click(object sender, EventArgs e)
        {
            AccountData acc = lst_accounts.SelectedItem as AccountData;
            if (acc == null)
            {
                UiUtils.getInstance().info("木有找到这个账号");
                return;
            }
            if (combo_servertype.SelectedIndex < 0)
            {
                UiUtils.getInstance().info("大虾，表糊弄我了，表还没填完呢，想进去免谈！！");
                return;
            }
            SMeta stype_meta = combo_servertype.SelectedItem as SMeta;
            if (stype_meta.Stype == ServerType.Custom)
            {
                if (txt_customlogin.Text == "" || txt_customgame.Text == "" || txt_customcode.Text == "")
                {
                    UiUtils.getInstance().info("大虾，表糊弄我了，表还没填完呢，想进去免谈！！");
                    return;
                }
            }
            if (num_serverid.Value <= 0 || txt_username.Text == "" || txt_password.Text == "")
            {
                UiUtils.getInstance().info("大虾，表糊弄我了，表还没填完呢！！");
                return;
            }

            int serverId = (int)(num_serverid.Value);
            string roleName = txt_rolename.Text;
            string username = txt_username.Text;
            string password = txt_password.Text;
            string customLoginUrl = txt_customlogin.Text;
            if (customLoginUrl != "" && customLoginUrl.IndexOf("http://") < 0 && customLoginUrl.IndexOf("https://") < 0)
            {
                customLoginUrl = "http://" + customLoginUrl;
            }
            else
            {
                customLoginUrl = "";
            }
            string customGameUrl = txt_customgame.Text;
            if (customGameUrl != "" && customGameUrl.IndexOf("http://") < 0 && customGameUrl.IndexOf("https://") < 0)
            {
                customGameUrl = "http://" + customGameUrl;
            }
            else
            {
                customGameUrl = "";
            }
            string customCode = txt_customcode.Text;
            acc.Server_type = stype_meta.Stype;
            acc.ServerId = serverId;
            acc.UserName = username;
            acc.Password = password;
            acc.RoleName = roleName;
            acc.CustomLoginUrl = customLoginUrl;
            acc.CustomGameUrl = customGameUrl;
            GlobalConfig.getInstance().saveSettings();
            GameMgr.removeGame(acc.generateGameId());
            GameMgr.addGame(acc);
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("是否确认删除账号??", "确认??", MessageBoxButtons.YesNo);
            if (result == System.Windows.Forms.DialogResult.Yes)
            {
                AccountData acc = lst_accounts.SelectedItem as AccountData;
                if (acc == null)
                {
                    UiUtils.getInstance().info("木有找到这个账号");
                    return;
                }
                GameMgr.removeGame(acc.generateGameId());
                GlobalConfig.getInstance().removeAccount(acc);
            }
        }

        private void btn_login_Click(object sender, EventArgs e)
        {
            GameMgr.startLogin();
        }

        private void combo_servertype_SelectedIndexChanged(object sender, EventArgs e)
        {
            SMeta meta = combo_servertype.SelectedItem as SMeta;
            if (meta == null) return;
            bool isCustom = meta.Stype == ServerType.Custom;
            txt_customcode.Enabled = txt_customgame.Enabled = txt_customlogin.Enabled = isCustom;
            txt_username.Enabled = txt_password.Enabled = txt_rolename.Enabled = num_serverid.Enabled = !isCustom;
        }

        private void lst_accounts_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lst_accounts.SelectedIndex < 0) return;
            AccountData acc = lst_accounts.SelectedItem as AccountData;
            if (acc == null) return;
            if (combo_servertype.Items == null) return;
            for (int i = 0, len = combo_servertype.Items.Count; i < len; i++)
            {
                SMeta meta = combo_servertype.Items[i] as SMeta;
                if (meta == null) continue;
                if (meta.Stype == acc.Server_type)
                {
                    combo_servertype.SelectedIndex = i;
                    break;
                }
            }
            num_serverid.Value = acc.ServerId;
            txt_username.Text = acc.UserName;
            txt_password.Text = acc.Password;
            txt_rolename.Text = acc.RoleName;
            txt_customcode.Text = acc.IsLastLogin.ToString();
            txt_customgame.Text = acc.CustomGameUrl;
            txt_customlogin.Text = acc.CustomLoginUrl;
        }

        private void btn_show_Click(object sender, EventArgs e)
        {
            if (lst_accounts.SelectedIndex < 0) return;
            AccountData acc = lst_accounts.SelectedItem as AccountData;
            if (acc == null) return;
            GameMgr.showGame(acc.generateGameId());
        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("真要退出吗? 所有账号将全部退出", "退出啦?", MessageBoxButtons.YesNo);
            if (result == System.Windows.Forms.DialogResult.Yes)
            {
                _intentExit = true;
                doGc();
                this.Close();
            }
        }
        private void doGc()
        {
            GameMgr.removeAllGames();
        }
        private void Selector_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!_intentExit)
            {
                this.Hide();
                e.Cancel = true;
            }
        }

        private void notify_icon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (!this.Visible)
            {
                this.Show();
                this.BringToFront();
            }
            else
            {
                this.Hide();
            }
        }

        private void lst_accounts_DoubleClick(object sender, EventArgs e)
        {
            if (lst_accounts.SelectedIndex < 0) return;
            AccountData acc = lst_accounts.SelectedItem as AccountData;
            if (acc == null) return;
            GameMgr.showGame(acc.generateGameId());
        }
    }
}
